import 'package:flutter/cupertino.dart';

//调用此方法，即可得到固定在顶部的SliverAppBar
SliverPersistentHeader setFFSliverAppBar({
  required Widget child,
  required double height,
  FFSliverParams? params,
}) {
  return SliverPersistentHeader(
    pinned: true,
    delegate: _SliverAppBarDelegate(
      child: child,
      height: height,
      params: params,
    ),
  );
}


class _FFAppBar extends StatelessWidget implements PreferredSizeWidget {
  Widget child;
  double height;

  _FFAppBar({
    Key? key,
    required this.child,
    required this.height,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return child;
  }

  @override
  Size get preferredSize => Size.fromHeight(height);
}

class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
  Widget child;
  double height;
  FFSliverParams? params;

  _SliverAppBarDelegate({
    required this.child,
    required this.height,
    this.params,
  });

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return _FFAppBar(
      height: height,
      child: child,
    );
  }

  @override
  double get maxExtent => params?.maxExtent ?? height;

  @override
  double get minExtent => params?.minExtent ?? height;

  @override
  bool shouldRebuild(_SliverAppBarDelegate oldDelegate) {
    return false;
  }
}

class FFSliverParams {
  double? maxExtent;
  double? minExtent;

  FFSliverParams({
    this.maxExtent = 0,
    this.minExtent = 0,
  });
}
